package com.gwtrpc.testapp.client;


import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.gwtrpc.testapp.shared.EbayDvdInfo;
import com.gwtrpc.testapp.shared.NytBuzz;
import com.gwtrpc.testapp.shared.NytMovie;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.layout.HStack;
import com.smartgwt.client.widgets.layout.VStack;



public class GWT_RPC implements EntryPoint {
	
	private static MyServiceAsync simpleService = GWT.create(MyService.class);
	
	private HorizontalPanel msgPanel = new  HorizontalPanel();
	private HorizontalPanel imagePanel;
	private HorizontalPanel contentPanel;
	private HorizontalPanel moviePanel;
	
	HStack ImageBar = new HStack(10);
	static VStack MovieBar = new VStack(1);
	
	private TabPanel tabPanel;
	private static FlowPanel tabSubPanel1;
	private static FlowPanel tabSubPanel2;
	private static int tabSelectedIndex;
	
	private static HorizontalPanel ebayInfoPanel;
	
	public static NytMovie[] sresult;
	
	
	private static int curMovieIdx;
	
	//private Label lblWait = new Label();
	
	public void onModuleLoad() {
		
		Page_load();

	}
	
	
	
	private void Page_load(){
		
		msgPanel.add(new HTML("Please wait while data is being fetched from NY Times ..."));
		msgPanel.setVisible(true);
		RootPanel.get("content").add(msgPanel);
		
		tabSelectedIndex = 0;	
		
		//Top Panel
		imagePanel = new HorizontalPanel();
		imagePanel.setStyleName("imagePanel");
		ImageBar.setLayoutMargin(10);  
        ImageBar.setLeft(120);  
       
        ImageBar.setShowEdges(true);  
        ImageBar.setCanAcceptDrop(true);  
        ImageBar.setAnimateMembers(true);  
        ImageBar.setShowDragPlaceHolder(true);  
        ImageBar.setBorder("1px solid #4040ff");  
       
        //MovieBar.setLayoutMargin(10);  
        MovieBar.setShowEdges(true);   
        MovieBar.setCanAcceptDrop(true);  
        MovieBar.setAnimateMembers(true);
        MovieBar.setHeight("400px");
        MovieBar.setWidth("250px");
        MovieBar.setTop(53);
       
        
   
        
        MovieBar.setDropLineThickness(4);  
	    Canvas dropLineProp = new Canvas();  
	    dropLineProp.setBackgroundColor("#40c040");  
	    MovieBar.setDropLineProperties(dropLineProp);  
		
	
		//Bottom Panel
		contentPanel = new HorizontalPanel();
		contentPanel.setStyleName("contentPanel");
		
		
		/****** Right hand side panels ******/
		

	    // Create a new tabbed Panel
		tabPanel = new TabPanel();
		tabPanel.setStyleName("halfWidth halfHeight tabPanel");
		
		//Ebay Info
		ebayInfoPanel = new HorizontalPanel();
		
		// Movie Review
		{
			tabSubPanel1 = new FlowPanel();
			tabSubPanel1.setHeight("400px");
			tabPanel.add(tabSubPanel1, "Details");
		}
	
		// Movie buzz
		{
			tabSubPanel2 = new FlowPanel();
			tabSubPanel2.setHeight("400px");
			tabPanel.add(tabSubPanel2, "Buzz");
		}
		
		/****** Left hand side panel ******/
		moviePanel = new HorizontalPanel();
		moviePanel.setStyleName("MoviePanel");
		moviePanel.setSize("200px", "400px");
		
		getNytMovie();
		
		tabPanel.addSelectionHandler(new SelectionHandler<Integer>(){ 
            public void onSelection(SelectionEvent<Integer> event) {
                if (event.getSelectedItem() != tabSelectedIndex) {
                	tabSelectedIndex = event.getSelectedItem();
                    
                	String movietitle = sresult[curMovieIdx].getTitle();
                	
                    switch(tabSelectedIndex)
                    {
                    case 0:
                    
                    	tabSubPanel1.clear();
						getEbayInfo(movietitle);	
						displayNytMovieDataSingle(sresult[curMovieIdx]);
                    	break;
                    	
                    case 1:
                    	tabSubPanel2.clear();
                    	getNytBuzz(movietitle);
                    	tabSubPanel2.add(new Image("http://www.duravit.com/duravit/file/all/ani_loader_big2.gif"));
                    	break;
                    }
                }
          }});
		
		
		//Image Panel
    	imagePanel.add(ImageBar);    
    	imagePanel.setVisible(true);
    	
    	
		RootPanel.get("content").add(imagePanel);
		
		//Movie Panel
		moviePanel.add(MovieBar);
		contentPanel.add(moviePanel);
		
		//Tab Panel
		tabPanel.selectTab(0);
		contentPanel.add(tabPanel);
		
		contentPanel.setVisible(true);
		RootPanel.get("content").add(contentPanel);
		
		
	}
	
	
	private void getNytMovie()
	 {
				if(simpleService==null)
					simpleService = GWT.create(MyService.class);
				
				AsyncCallback<NytMovie[]> callback = new AsyncCallback<NytMovie[]>(){
						public void onFailure(Throwable caught) {
							System.out.print(caught.getMessage());
							System.out.println("movie_load failure");
							displayErrorMessage(tabSubPanel1, "There was an error in retreiving Movie Information");
							
						}
						public void onSuccess(NytMovie[] result) {
							try{
								System.out.println("success");
								msgPanel.clear();
								msgPanel.add(new HTML("Here you go!"));
								
								if(result.length > 0){
									tabSubPanel1.clear();
									
									//Place in global DS
									sresult = result;
									
									System.out.println("before moviepanel");
									displayMoviePanel(sresult);
									System.out.println("after moviepanel");

									curMovieIdx = 0;												
									String movietitle = sresult[curMovieIdx].getTitle();
									getEbayInfo(movietitle);	
									displayNytMovieDataSingle(sresult[curMovieIdx]);
									String movieurl = sresult[curMovieIdx].getImage();
									MovieBar.addChild(new DroppedPiece(movieurl));
									
									
					
								} 
								
								
							}catch(Exception e){
								displayErrorMessage(tabSubPanel1, "There was an error in retreiving Movie Information");
							}
							
						}
						
					};
					simpleService.getNytMovie(callback);
			 
	 }
	 
	 //Movie Buzz
	 private static void getNytBuzz(String movietitle)
	 {
			if(simpleService==null)
				simpleService = GWT.create(MyService.class);	
			AsyncCallback<NytBuzz[]> callback = new AsyncCallback<NytBuzz[]>(){
				public void onFailure(Throwable caught) {
					System.out.print(caught.getMessage());
					System.out.println("movie_buzz failure");
					displayErrorMessage(tabSubPanel2, "There was an error in retreiving NYT Information");
					
				}
				public void onSuccess(NytBuzz[] result) {
					try{
						System.out.println("success");
						displayNytMovieBuzz(result);
						
						}catch(Exception e){
						displayErrorMessage(tabSubPanel2, "There was an error in retreiving NYT Information");
					}
					
				}

				};
					
				simpleService.getNytBuzz(movietitle,callback);
		 
	 }
	 
	 private static void getEbayInfo(String movietitle)
	 {
				if(simpleService==null)
					simpleService = GWT.create(MyService.class);
				
				AsyncCallback<EbayDvdInfo> callback = new AsyncCallback<EbayDvdInfo>(){
						public void onFailure(Throwable caught) {
							System.out.print(caught.getMessage());
							
							displayErrorMessage(tabSubPanel1, "There was an error in retreiving Ebay Movie Info");
							
						}
						public void onSuccess(EbayDvdInfo ebayDvdInfo) {
							try{
							
								displayEbayDvdInfo(ebayDvdInfo);
								
								}catch(Exception e){
								displayErrorMessage(tabSubPanel1, "There was an error in retreiving Ebay Movie Info");
							}
							
						}
						
					};
					simpleService.handleSimpleRequest(movietitle,callback);
			 
	 }
	 


	private static void displayNytMovieBuzz(NytBuzz[] summary)
	{
		
		tabSubPanel2.setStyleName("textHTML");
		
		HTML title,body;
		VerticalPanel buzz;
	
		for(int i = 0; i < summary.length; i++)
		{				
			title= new HTML("<h5><a href=\"" + summary[i].getUrl() + "\" target=\"_blank\">"+ summary[i].getTitle() + "</a><br>");			
			body = new HTML(summary[i].getBody() + "</h5>");
		
			buzz = new VerticalPanel();
			buzz.add(title);
			buzz.add(body);
			tabSubPanel2.add(buzz);	
	    }
	}
	
	private static void displayEbayDvdInfo(EbayDvdInfo ebayDvdInfo)
	{
		String title = ebayDvdInfo.getTitle();
		String galleryURL = ebayDvdInfo.getGalleryURL();
		String viewItemURL = ebayDvdInfo.getViewItemURL();
		String price = ebayDvdInfo.getPrice();	
				    						
		Image ebayImage = new Image(galleryURL);
		ebayImage.setHeight("122px");
		ebayImage.setWidth("100px");    						
				    						
		VerticalPanel ebayDetailPanel = new VerticalPanel();
		HTML headHtml = new HTML("<h3>eBay BestMatch Offer</h3>");
		HTML linkHtml = new HTML("<b><a href=\"" + viewItemURL  + "\" target=\"_blank\">" + title + "</a></b>");
		HTML priceHtml = new HTML("<h3>"+ price + "</h3>");
		ebayDetailPanel.add(headHtml);
		ebayDetailPanel.add(linkHtml);
		ebayDetailPanel.add(priceHtml);
		
		ebayInfoPanel.clear();
		ebayInfoPanel.add(ebayImage);
		ebayInfoPanel.add(ebayDetailPanel);  	
		
		tabSubPanel1.add(ebayInfoPanel);

	}
	
		
	private static void displayNytMovieDataSingle(NytMovie result){
		
		HTML title= new HTML("<h3>" + result.getTitle() + "</h3>");	
		HTML desc = new HTML("<h5>" + result.getDescription() + "</h5>");
		HTML overview = new HTML("<h5><a href=\"" + result.getOverview() + "\" target=\"_blank\">Movie Overview</a></h5>");		
		HTML review = new HTML("<h5><a href=\"" + result.getOverview() + "\" target=\"_blank\">NY Times Review</a></h5>");
		
		tabSubPanel1.add(title);
		tabSubPanel1.add(desc);
		tabSubPanel1.add(overview);
		tabSubPanel1.add(review);
	
	}
	
	
	private void displayMoviePanel(NytMovie[] summary){
		
		String url;
		
		for(int i = 0; i < summary.length; i++)
		{
			url = summary[i].getImage();
			ImageBar.addMember(new DragPiece(url,i));	
		}
		
	
	}
	
	private static void displayErrorMessage(Panel panel, String errorMsg)
	{
		panel.clear();
		panel.add(new HTML("<font size=\"5px\" color=\"red\"></font>"));
	}

	
	
	public static void doIT(int n){
		
		curMovieIdx = n;
		System.out.println("Before Assign");
		String movieTitle = sresult[curMovieIdx].getTitle();
		System.out.println("After Assign");
		switch(tabSelectedIndex){
			case 0: 
				tabSubPanel1.clear();
				getEbayInfo(movieTitle);	
				displayNytMovieDataSingle(sresult[curMovieIdx]);
			case 1: 
				tabSubPanel2.clear();
				getNytBuzz(movieTitle);
		}
	}
	
}
